package xiaohong.data.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class StackCountDemo {

    public static void main(String[] args) {

        String expr = "100 + 70 * 50 / 10 - 25 * 15".replace(" ", "");
        // 数字栈
        Stack<Integer> stackNumber = new Stack<>();
        // 运算符栈
        Stack<Integer> stackExpr = new Stack<>();
        int result = calculate(expr);
        System.out.println("result = " + result);
        List<String> exprList = parseExpression(expr);
        System.out.println("exprList = " + exprList);
//        exprList.forEach(value ->{
//            if (value instanceof Integer) {
//                stackNumber.push((Integer) value);
//            }
//            if (value instanceof String) {
//                int operatorCode = getOperatorCode((String) value);
//                if (stackExpr.isEmpty()){
//                    stackExpr.push((Integer) value);
//                }else {
//                    Integer exprNumber = stackExpr.peek();
//                    if (operatorCode >= exprNumber)   {
//
//                    }else  {
//
//                    }
//                }
//                stackExpr.push(Integer.parseInt((String) value));
//            }
//        });



    }


    public static List<String > parseExpression(String expr) {
        List<String> result = new ArrayList<>();
        int i = 0;
        while (i < expr.length()) {
            char c = expr.charAt(i);
            if (Character.isDigit(c)) {
                StringBuilder numBuilder = new StringBuilder();
                while (i < expr.length() && (Character.isDigit(c) || c == '.')) {
                    numBuilder.append(c);
                    i++;
                    if (i < expr.length()) {
                        c = expr.charAt(i);
                    }
                }
                result.add(numBuilder.toString());
            } else {
                result.add(String.valueOf(c));
                i++;
            }
        }
        return result;
    }

    public static int getOperatorCode(String operator) {
        if ("*".equals(operator) || "/".equals(operator)) {
            return 2;
        } else if ("+".equals(operator) || "-".equals(operator)) {
            return 1;
        }
        return -1; // 如果传入的不是预期的运算符，返回 -1作为错误标识（可根据实际需求调整错误处理方式）
    }


    public static int calculate(String expr) {
        Stack<Integer> operandStack = new Stack<>();
        Stack<Character> operatorStack = new Stack<>();

        for (int i = 0; i < expr.length(); i++) {
            char c = expr.charAt(i);
            if (Character.isDigit(c)) {
                int num = 0;
                while (i < expr.length() && Character.isDigit(c)) {
                    num = num * 10 + (c - '0');
                    i++;
                    if (i < expr.length()) {
                        c = expr.charAt(i);
                    }
                }
                i--; // 因为循环结束时多移动了一次i，所以回退一位
                operandStack.push(num);
            } else {
                while (!operatorStack.isEmpty() && hasHigherOrEqualPriority(operatorStack.peek(), c)) {
                    calculateTop(operandStack, operatorStack);
                }
                operatorStack.push(c);
            }
        }
//        System.out.println(hasHigherOrEqualPriority('*' , '/'));
        while (!operatorStack.isEmpty()) {
            calculateTop(operandStack, operatorStack);
        }

        return operandStack.pop();
    }

    // 10 + 70 * 50 / 10 - 25 * 15
    // +  op1 = +  op2 = *    return false
    // +  *  /  op1 = *  op2 = /    return false
    // +  *  /  op1 = /  op2 = -    return ture  计算
    public static boolean hasHigherOrEqualPriority(char op1, char op2) {
        if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
            return true;
        }
        return (op1 == op2);
    }




    public static void calculateTop(Stack<Integer> operandStack, Stack<Character> operatorStack) {
        int num2 = operandStack.pop();
        int num1 = operandStack.pop();
        char operator = operatorStack.pop();
        int result = switch (operator) {
            case '+' -> num1 + num2;
            case '-' -> num1 - num2;
            case '*' -> num1 * num2;
            case '/' -> num1 / num2;
            default -> 0;
        };
        operandStack.push(result);
    }


}
